﻿using System;
using System.Data;

namespace DAL
{
    public static class DalOrder
    {
        private static readonly clsConnect Cls = new clsConnect();
        #region[Get-Data]
        public static DataTable GetOrder(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_Order_Get");
        }
        public static DataTable GetOrderDetailEdit(int orderId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", orderId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_tb_Order_Detail_Get");
        }
        public static DataTable GetOrderDetailSum(int orderId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", orderId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Order_GetSum");
        }
        public static DataTable GetOrderDetailEditTmep(int flag, int usersId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", usersId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_tb_Order_Detail_Get_Temp");
        }
        public static DataTable GetOrderDetailEditTmepSum(int flag, int usersId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", usersId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_tb_Order_Detail_Get_TempSum");
        }
        public static DataTable GetOrderDetail_Edit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", id);
            return Cls.GetData("sp_tb_Order_Edit");
        }
        public static DataTable GetOrderActive(string keywords, int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            Cls.AddParameter("@IsActive", id);
            return Cls.GetData("sp_tb_Order_Fillter");
        }
        public static DataTable GetOrderFillterDate(string dateBegin, string dateEnd)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", dateBegin);
            Cls.AddParameter("@EndBegin", dateEnd);
            return Cls.GetData("sp_tb_Order_FillterDate");
        }
        public static DataTable GetOrderFillterIsUsed(string keywords, int isUsed)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            Cls.AddParameter("@IsUsed", isUsed);
            return Cls.GetData("sp_Order_GetFillter_IsUsed");
        }
        #endregion

        #region[Insert-Update-Delete]
        public static bool Insert(EtOrder obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Code", obj.Order_Code);
            Cls.AddParameter("@Order_TaxCode", obj.Order_TaxCode);
            Cls.AddParameter("@Order_Date", obj.Order_Date);
            Cls.AddParameter("@Customer_Id", obj.Customer_Id);
            Cls.AddParameter("@Order_DateBegin", obj.Order_DateBegin);
            Cls.AddParameter("@Order_DateEnd", obj.Order_DateEnd);
            Cls.AddParameter("@Order_Payment_Date", obj.Order_Payment_Date);
            Cls.AddParameter("@Order_Transport", obj.Order_Transport);
            Cls.AddParameter("@Order_Note", obj.Order_Note);
            Cls.AddParameter("@Order_VAT", obj.Order_VAT);
            Cls.AddParameter("@Order_Total", obj.Order_Total);
            Cls.AddParameter("@Order_Total_Payment", obj.Order_Total_Payment);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            return Cls.ExecuteNonQuery("sp_tb_Order_Insert");
        }
        public static bool Update(EtOrder obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            Cls.AddParameter("@Order_TaxCode", obj.Order_TaxCode);
            Cls.AddParameter("@Order_Note", obj.Order_Note);
            return Cls.ExecuteNonQuery("sp_Order_Update");
        }
        public static bool Delete(EtOrder obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Order_Delete", "@Msg");
            return true;
        }
        public static bool InsertDetail(EtOrderDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            Cls.AddParameter("@Materials_Id", obj.Materials_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_Price", obj.Order_Detail_Price);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_CK", obj.Order_Detail_CK);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            Cls.AddParameter("@Order_Detail_VAT", obj.Order_Detail_VAT);
            Cls.AddParameter("@Order_Detail_Total_Price", obj.Order_Detail_Total_Price);
            Cls.AddParameter("@Order_Detail_Note", obj.Order_Detail_Note);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_tb_Order_Detail_Insert");
        }
        public static bool DeleteDetail(EtOrderDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Detail_Id", obj.Order_Detail_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_Order_Detail_Delete");
        }
        public static bool UpdateQuanlity(EtOrderDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Detail_Id", obj.Order_Detail_Id);
            Cls.AddParameter("@Order_Detail_Quality", obj.Order_Detail_Quality);
            Cls.AddParameter("@Order_Detail_Price", obj.Order_Detail_Price);
            Cls.AddParameter("@Order_Detail_CK_Percent", obj.Order_Detail_CK_Percent);
            Cls.AddParameter("@Order_Detail_VAT_Percent", obj.Order_Detail_VAT_Percent);
            return Cls.ExecuteNonQuery("sp_Order_Detail_Update_Quanlity");
        }
        public static bool DeleteAdd(EtOrderDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", obj.Users_Id);
            Cls.AddParameter("@Flag", obj.Flag);
            return Cls.ExecuteNonQuery("sp_Order_Detail_Delete_Add");
        }
        public static bool GetOrderCheck(EtOrder obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_CheckOrderEdit", "@Msg");
            return true;
        }
        public static bool GetOrderCheckReview(EtOrder obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_CheckOrderReviewEdit", "@Msg");
            return true;
        }
        #endregion

        #region[Review]
        public static bool Review(EtOrderDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Id", obj.Order_Id);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@Users_Id_Review", obj.Users_Id_Review);
            return Cls.ExecuteNonQuery("sp_Order_Detail_Review");
        }
        #endregion
    }

    public class EtOrder
    {
        public int Users_Id { get; set; }
        public string Order_TaxCode { get; set; }
        public bool IsActive { get; set; }
        public int Order_Id { get; set; }
        public string Order_Code { get; set; }
        public DateTime Order_Date { get; set; }
        public int Customer_Id { get; set; }
        public DateTime Order_DateBegin { get; set; }
        public DateTime Order_DateEnd { get; set; }
        public DateTime Order_Payment_Date { get; set; }
        public float Order_Transport { get; set; }
        public string Order_Note { get; set; }
        public float Order_VAT { get; set; }
        public float Order_Total { get; set; }
        public float Order_Total_Payment { get; set; }
        public int Msg { get; set; }
    }
    public class EtOrderDetail
    {
        public int ? Users_Id_Review { get; set; }
        public DateTime DateReview { get; set; }
        public bool IsActive { get; set; }
        public float ? Order_Detail_VAT { get; set; }
        public float ? Order_Detail_VAT_Percent { get; set; }
        public float ? Order_Detail_CK { get; set; }
        public float ? Order_Detail_CK_Percent { get; set; }
        public string Order_Detail_Note { get; set; }
        public float ? Order_Detail_Total_Price { get; set; }
        public float ? Order_Detail_Price { get; set; }
        public int ? Order_Detail_Quality { get; set; }
        public int ? Materials_Id { get; set; }
        public int ? Order_Id { get; set; }
        public int ? Order_Detail_Id { get; set; }
        public int ? Users_Id { get; set; }
        public int ? Flag { get; set; }
    }
}